<?php

/*
 * 推荐微博的相关定义
 */

/**********************
* 文件包含
*********************/
include_once('./data/tables.php');
include_once('./api/api_db.php');

/**********************
* 类定义
*********************/
/**
 * 公共墙上的一条微博的定义
 */
class OneSuggest {
    public $id;     //suggest表中的id
    public $weibo_id;
    public $weibo_mid;
    public $latitude;
    public $longtitude;
    public $suggest_user;
    public $like_num;
    public $to_group;
    public $add_time;
    public $is_like;        //是否已经赞过
    public $weibo_text;     //微博正文
    public $weibo_nickname; //作者昵称
    
    public function __construct($id, $weibo_id, $weibo_mid, 
                                 $latitude, $longtitude, $suggest_user, 
                                 $like_num, $to_group, $add_time, 
                                 $is_like, $weibo_text, $weibo_nickname) {
        $this->id = $id;
        $this->weibo_id = $weibo_id;
        $this->weibo_mid = $weibo_mid;
        $this->latitude = $latitude;
        $this->longtitude = $longtitude;
        $this->suggest_user = $suggest_user;
        $this->like_num = $like_num;
        $this->to_group = $to_group;
        $this->add_time = $add_time;
        $this->is_like = $is_like;
        $this->weibo_text = $weibo_text;
        $this->weibo_nickname = $weibo_nickname;
    }
}
/**********************
* 方法实现
*********************/
/**
 * 推荐一条微博
 */
function suggest_add($weibo_id, $weibo_mid, $latitude, $longtitude, $suggest_user, $weibo_text, $weibo_nickname, $group = 0) {
    global $glb_tb_suggest;
    
    //判断经纬度
    if(!isset($weibo_id) || !isset($weibo_mid) ||!isset($latitude) || !isset($longtitude) || !isset($suggest_user)) {
        $arr = array(
            FLAG_RET => ERROR,
            FLAG_ERR_CODE => ERR_VAL
        );
        
        return $arr;
    }
    
    //检查是否已经存在
    $where = COL_WEIBO_MID . '= "' . $weibo_mid . '"';
    if(db_is_exist($glb_tb_suggest, $where)) {
        $arr = array(
            FLAG_RET => ERROR,
            FLAG_ERR_CODE => ERR_EXIST
        );
        
        return $arr;
    }
    
    //记录时间  
    date_default_timezone_set("PRC");  
    $add_time = date("Y-m-d H:i:s");   //2010-03-06 11:51:29 
    
    //在数据库中添加记录
    $table_row = "(" . COL_WEIBO_ID . "," .COL_WEIBO_MID . "," . COL_LATITUDE . "," . COL_LONGTITUDE . "," . 
                       COL_SUGGEST_USER . "," . COL_LIKE_NUM . "," . COL_GROUP . "," .COL_ADD_TIME . "," .
                       COL_WEIBO_TEXT . "," . COL_WEIBO_NICKNAME . ")";
    $table_content = "('$weibo_id' ,'$weibo_mid' , '$latitude' , '$longtitude' , 
                       '$suggest_user' , '0' , '$group' ,'$add_time' , 
                       '$weibo_text' , '$weibo_nickname')";
    if(FALSE == db_run_insert($glb_tb_suggest, $table_row, $table_content)) {
        $arr = array(
            FLAG_RET => ERROR,
            FLAG_ERR_CODE => ERR_DB,
            FLAG_ERR_DETAIL => mysql_error()
        );
        
        return $arr;
    } else {
        $arr = array(
            FLAG_RET => NO_ERR
        );
        
        return $arr;
    }
}

/**
 * 读取公共墙上的所有微博
 * @param type $user_id 当前登录的用户id（不是新浪微博的id）
 */
function suggest_get($user_id) {
    global $glb_tb_suggest;
    global $glb_tb_like;
    
    $target = "*";
    $result = db_run_select($glb_tb_suggest,
                            $target);
    if($result == FALSE) {
        $arr = array(
            FLAG_RET => ERROR,
            FLAG_ERR_CODE => ERR_DB,
            FLAG_ERR_DETAIL => mysql_error()
        );
        
        return $arr;
    }
    
    $arr = array();
    $i = 0;

    while($row = mysql_fetch_array($result))
    {
        $id = $row[COL_ID];
        $weibo_id = $row[COL_WEIBO_ID];
        $weibo_mid = $row[COL_WEIBO_MID];
        $latitude = $row[COL_LATITUDE];
        $longtitude = $row[COL_LONGTITUDE];
        $suggest_user = $row[COL_SUGGEST_USER];
        $like_num = $row[COL_LIKE_NUM];
        $to_group = $row[COL_GROUP];
        $add_time = $row[COL_ADD_TIME];
        $weibo_text = $row[COL_WEIBO_TEXT];
        $weibo_nickname = $row[COL_WEIBO_NICKNAME];
        
        $where = COL_SUGGEST_ID . "= '" . $id . "' AND " . COL_USER_ID . "= '" . $user_id . "'";
        if(db_is_exist($glb_tb_like, $where)) {
            $is_like = 1;
        } else {
            $is_like = 0;
        }
        
        $arr[$i] = new OneSuggest($id, $weibo_id, $weibo_mid, 
                                  $latitude, $longtitude, $suggest_user, 
                                  $like_num, $to_group, $add_time, 
                                  $is_like, $weibo_text, $weibo_nickname);
        $i++;
    }

    $ret = array(
        FLAG_RET => NO_ERR,
        FLAG_STATUSES => $arr
    );
    
    return $ret;
}

?>
